/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.hamacekj.geometr;

import cz.hamacekj.geometr.plugin.ConstructionObject;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.util.Map;
import javax.swing.ImageIcon;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;

/**
 * Renderer záznamů postupu konstrukce.
 * @author hamacekh
 */
public class ListStepRenderer implements ListCellRenderer<Step> {
    
    private Map<String, UIConstructionObject> plugins;

    public ListStepRenderer(Map<String, UIConstructionObject> plugins) {
        this.plugins = plugins;
    }
    
    @Override
    public Component getListCellRendererComponent(
            JList<? extends Step> list,
            Step value,
            int index,
            boolean isSelected,
            boolean cellHasFocus) {
        return this.getRenderComponent(list, value, index, isSelected, cellHasFocus);
    }
    
    public ListStep getRenderComponent(JList<? extends Step> list,
            Step value,
            int index,
            boolean isSelected,
            boolean cellHasFocus) {
        ConstructionObject[] objects = value.getConstructionObjects();
        String stepText = objects[0].getStepDescription();
        stepText = stepText.replace("<\\in>", "\u2208")
                .replace("<\\elem>", "\u2208")
                .replace("<\\notin>", "\u2209")
                .replace("<\\notelem>", "\u2209")
                .replace("<\\and>", "\u2227")
                .replace("<\\or>", "\u2228")
                .replace("<\\union>", "\u222A")
                .replace("<\\intersection>", "\u2229")
                .replace("<\\perp>", "\u22A5")
                .replace("<\\perpendicular>", "\u22A5")
                .replace("<\\parallel>", "\u2225");
        String[] splitted = stepText.split("<");
        ImageIcon[] images = new ImageIcon[splitted.length];
        String[] text = new String[splitted.length];
        for (int i = 0; i < splitted.length; i++){
            String[] separated = splitted[i].split(">", 2);
            boolean contains = splitted[i].contains(">");
            if(separated.length == 1){
                text[i] = separated[0];
                images[i] = null;
            }else{
                assert separated.length > 1;
                text[i] = separated[1];
                images[i] = getImage(separated[0]);
            }
        }
        return new ListStep(text, images, isSelected);
    }
    
    private ImageIcon getImage(String name){
        UIConstructionObject p = plugins.get(name);
        if(p == null){
            return null;
        }
        return p.getIcon();
    }
    
}
